package com.jcf.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcf.springboot.entity.business.Evaluation;
import com.jcf.springboot.entity.business.vo.UserEvalution;
import com.jcf.springboot.mapper.EvaluationMapper;
import com.jcf.springboot.service.EvaluationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcf.springboot.entity.business.Member;
import com.jcf.springboot.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jjj
 * @since 2021-04-11
 */
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {

    @Autowired
    private MemberService memberService;

    @Override
    public List<UserEvalution> getListByFilm(String filmId) {
        QueryWrapper<Evaluation> wrapper = new QueryWrapper<>();
        wrapper.eq("film_id", filmId);
        wrapper.orderByDesc("eva_date");
        List<Evaluation> list = this.baseMapper.selectList(wrapper);
        ArrayList<UserEvalution> userEvalutions = new ArrayList<>();
        for (Evaluation evaluation : list) {
            UserEvalution userEvalution = new UserEvalution();
            BeanUtils.copyProperties(evaluation, userEvalution);
            System.out.println(userEvalution);
            QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
            memberQueryWrapper.eq("member_id", evaluation.getMemberId());
            Member member = memberService.getOne(memberQueryWrapper);
            if(member != null){
                BeanUtils.copyProperties(member,userEvalution);
            }
            userEvalutions.add(userEvalution);
        }
        return userEvalutions;
    }
}
